#! /usr/bin/perl -w

# script for optimizing performance (sleep time based on excution time of main command)
# --logfile <NONE>/filename
# --optimize 0/1

#................................................................................................... SUBS Declaration
sub RELOAD_RC ;
sub FOOTER ;
#................................................................................................... MAIN BODY
#................................................. ADD Control string to rc-file
$ID = 1000000000 + int rand 8999999999 ;
open (RC_FILE, ">>$ENV{'HOME'}/.refreshrc") || die "Vj: Can't open FILE: $ENV{'HOME'}/.refreshrc for appending\nSystem: $!";
print RC_FILE "$ID 60.00 RUNNING $ENV{PWD} '@ARGV'\n";
close RC_FILE ;
#................................................................................................... MAIN BODY
while (1)
{
        RELOAD_RC ;
        if (lc($STATUS) eq "kill") {
                system "sed -i '/$ID/d' $ENV{'HOME'}/.refreshrc";
                exit ;
        }
        system ("( @ARGV > /dev/tty ) >& /dev/null") ;
        FOOTER ;
        sleep $INTERVEL ;
}
#................................................................................................... RELOAD_RC
sub RELOAD_RC {
        open (RC_FILE, "<$ENV{'HOME'}/.refreshrc") || die "Vj: Can't open FILE: $ENV{'HOME'}/.refreshrc for reading\nSystem: $!";
        while (<RC_FILE>) {
                @WORDS = split (" ", $_) ;
                if ( $WORDS[0] =~ /$ID/ ) {
                        $STATUS = $WORDS[2];
                        $INTERVEL = $WORDS[1];
                }
        }
        close RC_FILE ;
}
#................................................................................................... FOOTER
sub FOOTER {
        print "\n-----------------Update-----------------\n" ;
        print " Updated: " ;
        system ("date") ;
        print "CTRL ID : $ID    INTERVEL: $INTERVEL sec\n" ;
}
